package com.auth.controller;
import com.auth.aop.OpLog;
import com.auth.convert.StrToListFormatter;
import com.auth.entity.dto.AuthorityDto;
import com.auth.entity.dto.RoleDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import com.auth.service.IUserRoleService;
import com.auth.entity.vo.UserRoleVo;
import com.auth.entity.dto.UserRoleDto;
import org.springframework.validation.DataBinder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.easyCode.feature.mybaits.CustomPage;
import com.easyCode.feature.result.ResponseResultAdvice;
import java.util.List;
import io.swagger.annotations.ApiOperation;

import javax.validation.constraints.Positive;

@Validated
@RestController
@RequestMapping("/userRole")
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags = "用户角色请求")
public class UserRoleController {
    private final IUserRoleService iUserRoleService;

    @InitBinder
    public void initBinder(DataBinder dataBinder){
        dataBinder.addCustomFormatter(new StrToListFormatter());
    }

    @ApiOperation("获取用户角色")
    @PostMapping("/{userId}")
    public List<RoleDto> getRoleOfUser(@PathVariable("userId")@Positive(message = "id不合法")Integer userId){
        return iUserRoleService.getRoleOfUser(userId);
    }

    @OpLog(desc ="角色%s赋予ID为%s的用户")
    @ApiOperation("赋予用户角色")
    @PostMapping("/releaseRoleToUser/{userId}")
    public void releaseRoleToUser(@RequestParam("roleList")@ApiParam("角色id列表,由'-'分割角色id,如: 1-2-3")List<Integer> authorityList, @PathVariable("userId")@Positive(message = "id不合法")Integer userId) {
        iUserRoleService.releaseRoleToUser(userId,authorityList);
    }

    @OpLog(desc = "角色%s从ID为%s的用户处回收")
    @ApiOperation("收回用户角色")
    @GetMapping("/revokeRoleFromUser/{userId}")
    public void revokeRoleFromUser(@RequestParam("roleList")@ApiParam("角色id列表,由'-'分割角色id,如: 1-2-3") List<Integer> authorityList, @PathVariable("userId")@Positive(message = "id不合法")Integer userId) {
        iUserRoleService.revokeRoleFromUser(userId,authorityList);
    }
}
